<script>
import { reminderManager } from './utils/reminder'
import { syncManager } from './utils/sync'

export default {
	onLaunch: function() {
		console.log('App Launch')
		
		// 检查是否有token，记录日志
		const token = uni.getStorageSync('token');
		console.log('启动时检查Token:', token ? '已存在' : '不存在');
		
		// 初始化提醒系统
		console.log('初始化提醒系统...')
		reminderManager.initializeReminders()
		
		// 初始化同步系统
		syncManager.initialize()
		this.checkLogin()
		
		// 设置默认提醒配置（如果不存在）
		const settings = uni.getStorageSync('settings')
		if (!settings) {
			const defaultSettings = {
				enableNotification: true, // 默认开启提醒
				reminderTime: 1, // 默认提前15分钟提醒
				notificationType: 1, // 默认声音+弹窗提醒
				darkMode: false, // 默认浅色模式
				language: 'zh-CN' // 默认中文
			}
			uni.setStorageSync('settings', defaultSettings)
			console.log('已设置默认提醒配置')
		}
	},
	onShow: function() {
		console.log('App Show')
	},
	onHide: function() {
		console.log('App Hide')
	},
	methods: {
		checkLogin() {
			// 获取当前页面路径
			const pages = getCurrentPages()
			const currentPage = pages[pages.length - 1]
			const currentRoute = currentPage ? currentPage.route : ''
			console.log('当前页面路径:', currentRoute);
			
			// 不需要登录的页面
			const noAuthPages = ['pages/login/login']
			
			// 检查token
			const token = uni.getStorageSync('token')
			
			// 如果当前不在登录页，且没有token，跳转到登录页
			if (!noAuthPages.includes(currentRoute) && !token) {
				console.log('未登录，跳转到登录页');
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}
		}
	}
}
</script>

<style>
	/*每个页面公共css */
	/* 全局样式 */
	page {
		background-color: #f5f5f5;
	}

	/* 通用样式类 */
	.container {
		flex: 1;
		display: flex;
		flex-direction: column;
	}

	.section {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;
	}

	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 20rpx;
	}

	.btn {
		padding: 20rpx 40rpx;
		border-radius: 40rpx;
		font-size: 28rpx;
		text-align: center;
	}

	.btn-primary {
		background-color: #007AFF;
		color: #fff;
	}

	.btn-default {
		background-color: #f5f5f5;
		color: #333;
	}

	.text-primary {
		color: #007AFF;
	}

	.text-danger {
		color: #ff4d4f;
	}

	.text-success {
		color: #52c41a;
	}

	.text-warning {
		color: #faad14;
	}

	.text-gray {
		color: #999;
	}

	.flex-row {
		display: flex;
		flex-direction: row;
	}

	.flex-column {
		display: flex;
		flex-direction: column;
	}

	.justify-between {
		justify-content: space-between;
	}

	.align-center {
		align-items: center;
	}

	.margin-top-sm {
		margin-top: 10rpx;
	}

	.margin-top {
		margin-top: 20rpx;
	}

	.margin-bottom-sm {
		margin-bottom: 10rpx;
	}

	.margin-bottom {
		margin-bottom: 20rpx;
	}

	.padding-sm {
		padding: 10rpx;
	}

	.padding {
		padding: 20rpx;
	}

	.border-bottom {
		border-bottom: 1px solid #eee;
	}
</style>
